package dao.Impl;

import dao.ProductDao;
import dto.ShowDto;
import model.ProductModel;
import util.JDBC;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class ProductImpl implements ProductDao {
    @Override
    public List<ProductModel> getNameList() {
        String sql = "select * from `hs_producttitle`";
        ResultSet r = JDBC.getResultSet(sql);
        List<ProductModel> list = new ArrayList<>();
        try {
            while (r.next()) {
                ProductModel model = new ProductModel();
                model.setId(r.getInt("id"));
                model.setNameTitle(r.getString("title"));
                model.setNameHref(r.getString("href"));
                list.add(model);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBC.close();
        }
        return list;
    }

    @Override
    public List<ShowDto> getShowList() {
        String sql = "";
        ResultSet r = JDBC.getResultSet(sql);
        List<ShowDto> list = new ArrayList<>();
        try {
            while (r.next()) {
                ShowDto model = new ShowDto();
                model.setId(r.getInt("id"));
                model.setProductImg(r.getString("img"));
                model.setProductName(r.getString("title"));
                model.setProductPrice(r.getString("href"));
                model.setProductMaterial(r.getString("id"));
                list.add(model);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBC.close();
        }
        return list;
    }


    public ProductModel getContent() {
        String sql = "select * from `hs_producttitle`";
        ResultSet r = JDBC.getResultSet(sql);
        ProductModel productModel = null;
        try {
            while (r.next()) {
                productModel = new ProductModel();
                //resultSet.getString() 对应数据库的字段名称
                productModel.setId(r.getInt("id"));
                productModel.setImg(r.getString("img"));
                productModel.setProductName(r.getString("name"));
                productModel.setMarkerPrice(r.getString("markerPrice"));
                productModel.setPrice(r.getString("price"));
                productModel.setPart(r.getString("part"));
                productModel.setAssist(r.getString("assist"));
                productModel.setSeasoning(r.getString("seasoning"));

            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBC.close();
        }
        return productModel;
    }
}
